//
// Copyright (c) 2009 All Right Reserved
//
// vl
//
// 2018-12-01
// Contains ...
namespace LargoPanels.Editor
{
using LargoCommon.Interfaces;
using LargoCommon.Music;
using LargoPanels.Abstract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Xml.Linq;
///
/// Interact logic for LineCell.
///
public class LineCell : BaseCell
{
#region Constructors
///
/// Initializes a new instance of the class.
///
/// The given master.
/// The given line.
public LineCell(EditorSpace givenMaster, IAbstractLine givenLine) : base(givenMaster) {
this.VoiceCells = new List();
this.LineIndex = givenLine.LineIndex;
this.Line = givenLine;
this.Point = new MusicalPoint(this.Line.LineIndex, -1);
}
#endregion
#region Main Properties
/// Gets or sets the voice cells.
/// The voice cells.
public List VoiceCells { get; set; }
/// Gets the main voice cell.
/// The main voice cell.
public VoiceCell MainVoiceCell {
get {
var vc = this.VoiceCells.FirstOrDefault();
return vc;
}
}
#endregion
#region Main Properties
/// Gets or sets the instrument.
/// The instrument.
public MusicalInstrument Instrument {
get => this.Line.FirstStatus.Instrument;
set {
this.Line.FirstStatus.Instrument = value;
if (this.MainVoiceCell != null) {
this.MainVoiceCell.Voice.Instrument = value;
}
}
}
/// Gets or sets the octave.
/// The octave.
public MusicalOctave Octave {
get => this.Line.FirstStatus.Octave;
set {
this.Line.FirstStatus.Octave = value;
if (this.MainVoiceCell != null) {
this.MainVoiceCell.Voice.Octave = value;
}
}
}
/// Gets or sets the loudness.
/// The loudness.
public MusicalLoudness Loudness {
get => this.Line.FirstStatus.Loudness;
set {
this.Line.FirstStatus.Loudness = value;
if (this.MainVoiceCell != null) {
this.MainVoiceCell.Voice.Loudness = value;
}
}
}
#endregion
#region Properties
///
/// Gets or sets the track.
///
///
/// The track.
///
public IAbstractLine Line { get; set; }
#endregion
///
/// Gets the context menu of line purpose.
///
///
/// The context menu of line purpose.
///
public ContextMenu ContextMenuOfLinePurpose {
get {
var contextMenu = new ContextMenu {
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch
};
//// Lines
var item = new MenuItem { Header = "Mark as composed", Tag = 2, HorizontalAlignment = HorizontalAlignment.Center }; //// LocalizedControls.String
item.Click += this.MarkLinePurpose;
contextMenu.Items.Add(item);
item = new MenuItem { Header = "Mark as fixed", Tag = 1, HorizontalAlignment = HorizontalAlignment.Center }; //// LocalizedControls.String
item.Click += this.MarkLinePurpose;
contextMenu.Items.Add(item);
item = new MenuItem { Header = "Mark as muted", Tag = 0, HorizontalAlignment = HorizontalAlignment.Center }; //// LocalizedControls.String
item.Click += this.MarkLinePurpose;
contextMenu.Items.Add(item);
return contextMenu;
}
}
#region Public methods
///
/// Sets the data.
///
/// The given data.
/// Returns value.
public bool SetData(IDataObject givenData) {
var space = this.Master as EditorSpace;
if (space != null) {
var block = space.MusicalContent as MusicalBlock;
if (givenData.GetData("MelodicInstrument") is MelodicInstrument melodicInstrument) {
var instrument = new MusicalInstrument((MidiMelodicInstrument)melodicInstrument.Id);
this.Instrument = instrument;
if (block != null) {
var elements = block.Body.ElementsOfLine(this.Line.LineIdent).ToList();
MusicalBody.SetInstrumentToElements(instrument, elements);
}
return true;
}
if (givenData.GetData("RhythmicInstrument") is RhythmicInstrument rhythmicInstrument) {
var instrument = new MusicalInstrument((MidiRhythmicInstrument)rhythmicInstrument.Id);
this.Instrument = instrument;
if (block != null) {
var elements = block.Body.ElementsOfLine(this.Line.LineIdent).ToList();
MusicalBody.SetInstrumentToElements(instrument, elements);
}
return true;
}
if (givenData.GetData("MusicalOctave") is MusicalOctave musicalOctave) {
this.Octave = musicalOctave;
if (block != null) {
var elements = block.Body.ElementsOfLine(this.Line.LineIdent).ToList();
MusicalBody.SetOctaveToElements(musicalOctave, elements);
}
return true;
}
if (givenData.GetData("MusicalLoudness") is MusicalLoudness musicalLoudness) {
this.Loudness = musicalLoudness;
if (block != null) {
var elements = block.Body.ElementsOfLine(this.Line.LineIdent).ToList();
MusicalBody.SetLoudnessToElements(musicalLoudness, elements);
}
return true;
}
}
return false;
}
/// Gets or sets the formatted text.
/// The formatted text.
public override FormattedText FormattedText() {
var sb = new StringBuilder();
if (this.Line?.FirstStatus != null) { //// this.VoiceCells != null && this.MainVoiceCell != null)
sb.AppendFormat("{0}/ {1} {2}\n", this.Line.LineNumber, this.Octave, this.Loudness);
sb.Append(this.Instrument);
}
var ft = AbstractText.Singleton.FormatText(sb.ToString(), (int)this.Width - SeedSize.BasicMargin);
//// var s = string.Format("{0,2} {1}", this.Line?.LineNumber, CultureMaster.Localize(this.Line?.Purpose.ToString()));
return ft;
}
#endregion
#region Copy-Paste
///
/// Copies this instance.
///
public override void Copy() {
if (this.MainVoiceCell == null) {
return;
}
StringBuilder sb = new StringBuilder();
var xstatus = this.MainVoiceCell.GetXElement;
var item = xstatus.ToString();
sb.AppendFormat("{0};", item);
Clipboard.SetText(sb.ToString());
//// Clipboard.SetDataObject(xstatus);
Console.Beep(880, 180);
}
///
/// Pastes this instance.
///
public override void Paste() {
if (this.MainVoiceCell == null) {
return;
}
var s = Clipboard.GetText();
if (string.IsNullOrEmpty(s)) {
return;
}
var splitArray = s.Split(';');
if (!splitArray.Any()) {
return;
}
var item = splitArray.First();
var xstatus = XElement.Parse(item);
//// var header = this.Master.GetMusicalHeader;
//// var status = new LineStatus(xstatus, header);
this.MainVoiceCell.SetXElement(xstatus);
//// var xstatus = XElement.Parse(item);
//// this.Refresh();
Console.Beep(990, 180);
//// this.RedrawCell(false);
var space = this.Master as EditorSpace;
space?.InvalidateVisual();
}
#endregion
#region Mark tracks
///
/// Marks the tracks.
///
/// The sender.
/// The instance containing the event data.
public void MarkLinePurpose(object sender, RoutedEventArgs e) {
var menuItem = (MenuItem)sender;
switch ((int)menuItem.Tag) {
case 0:
this.Line.Purpose = LinePurpose.Mute;
break;
case 1:
this.Line.Purpose = LinePurpose.Fixed;
break;
case 2:
this.Line.Purpose = LinePurpose.Composed;
break;
}
var space = this.Master as EditorSpace;
if (space != null && space.MusicalContent is MusicalBlock block) {
var elements = block.Body.ElementsOfLine(this.Line.LineIdent).ToList();
MusicalBody.SetPurposeToElements(this.Line.Purpose, elements);
}
//// this.Refresh();
}
#endregion
}
}